package LatestDataAndOutOfOrderWaterMark;


import bean.WaterSensor;


import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.TimeCharacteristic;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

import java.time.Duration;
import java.util.Arrays;

/**
 * @author Spring_Hu
 * @date 2021/10/14 21:18
 */
public class LatestDataAndOutOfOrderWaterMark {
    /*
    *
    * 有乱序时间 有迟到时间  有测输出流
    *
    * */
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        SingleOutputStreamOperator<WaterSensor> source = env
                .socketTextStream("localhost", 9999)
                .map(new MapFunction<String, WaterSensor>() {
                    @Override
                    public WaterSensor map(String value) throws Exception {
                        String[] line = value.split(",");
                        return new WaterSensor(
                                line[0],
                                Long.parseLong(line[1]),
                                Integer.parseInt(line[2]));
                    }
                });

        SingleOutputStreamOperator<WaterSensor> sensorDs = source.assignTimestampsAndWatermarks(
                WatermarkStrategy
                        .<WaterSensor>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                        .withTimestampAssigner(new SerializableTimestampAssigner<WaterSensor>() {
                            @Override
                            public long extractTimestamp(WaterSensor element, long recordTimestamp) {
                                return element.getTs()*1000L;
                            }
                        })
        );

        WindowedStream<WaterSensor, String, TimeWindow> sensorWS = sensorDs
                .keyBy(sensor -> sensor.getId())
                .window(TumblingEventTimeWindows.of(Time.seconds(5))) // 1.开窗之后就没有时间触发器了？？？
                                                                      //2.定时器的触发时间你了解了吗？
                .allowedLateness(Time.seconds(3))
                .sideOutputLateData(new OutputTag<WaterSensor>("late"){});

        // TODO 全窗口函数
        SingleOutputStreamOperator<String> resultDS = sensorWS.process(new ProcessWindowFunction<WaterSensor, String, String, TimeWindow>() {
            @Override
            public void process(String s, Context context, Iterable<WaterSensor> elements, Collector<String> out) throws Exception {

                out.collect("key=" + s + "\n"
                        + "窗口有" + elements.spliterator().estimateSize() + "条数据" + "\n"
                        + "数据为：" + Arrays.asList(elements).toString() + "\n"
                        + "窗口划分:[" + context.window().getStart() + "," + context.window().getEnd() + ")" + "\n\n"
                );
            }
        });

        resultDS.print("result>>>>>");

        resultDS.getSideOutput(new OutputTag<WaterSensor>("late"){}).print("late>>>>");

        env.execute();
    }
}
